package com.sinoatma.physignsmonitor.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.PermissionChecker
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.common.frame.adapter.NavigationAdapter
import com.common.frame.base.BaseActivity
import com.common.frame.base.CommonFragmentStateAdapter
import com.common.frame.bean.NavigationItem
import com.sinoatma.physignsmonitor.MyApp
import com.sinoatma.physignsmonitor.R
import com.sinoatma.physignsmonitor.constant.BloodTransfusionModeFlag
import com.sinoatma.physignsmonitor.constant.alarmRecord1
import com.sinoatma.physignsmonitor.constant.alarmRecord2
import com.sinoatma.physignsmonitor.constant.alarmRecordDate
import com.sinoatma.physignsmonitor.databinding.ActivityMainBinding
import com.sinoatma.physignsmonitor.fragment.*
import java.util.Date

/**
 * @ProjectName : PhySignsMonitor
 * @Author :
 * @Time : 2024/12/27 17:38
 * @Description : 文件描述
 */
class MainActivity : BaseActivity<MainViewModel,ActivityMainBinding>() {
    override fun fitsSystemWindows(): Boolean =false
    override fun showTitleBar(): Boolean = false
    lateinit var fragments: MutableList<Fragment>
    lateinit var navigationAdapter: NavigationAdapter

    override fun layoutId(): Int =R.layout.activity_main

    override fun initData() {
        fragments = if (BloodTransfusionModeFlag ==1){
            mutableListOf(
                MonitorFragment(),
                HistoryListFragment(),
                DeviceManagementFragment(),
                SystemSettingFragment())
        }else{
            mutableListOf(
                BloodTransFragment(),
                HistoryListFragment(),
                DeviceManagementFragment(),
                SystemSettingFragment())
        }
        val navigationList = mutableListOf<NavigationItem>().apply {
            add(NavigationItem(R.mipmap.icon_monitor, R.mipmap.icon_monitor, "实时监控"))
            add(NavigationItem(R.mipmap.icon_history, R.mipmap.icon_history, "历史列表"))
            add(NavigationItem(R.mipmap.icon_system, R.mipmap.icon_system, "设备管理"))
            add(NavigationItem(R.mipmap.icon_settings, R.mipmap.icon_settings, "系统设置"))
        }
        navigationAdapter = NavigationAdapter(navigationList)
        binding.recyclerView.adapter = navigationAdapter
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        navigationAdapter.setOnItemClickListener { _, _, position ->
            navigationAdapter.setSelect(position)
            binding.viewPager.setCurrentItem(position, false)
        }
        val fragmentStateAdapter = CommonFragmentStateAdapter(this, fragments)
        binding.viewPager.adapter = fragmentStateAdapter
        binding.viewPager.isUserInputEnabled = false
        binding.viewPager.offscreenPageLimit = fragments.size

        val flags: Int = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        window.decorView.systemUiVisibility = flags

        if (alarmRecordDate.isEmpty()){
            alarmRecordDate=viewModel.sdfDate.format(Date())
        }else{
          if (alarmRecordDate != viewModel.sdfDate.format(Date())){
             alarmRecordDate=viewModel.sdfDate.format(Date())
             alarmRecord1=""
             alarmRecord2=""
          }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.online()
    }

    override fun initView() {
        if (BloodTransfusionModeFlag==0){
            binding.tvSysName.text="智慧输血监护系统"
        }else{
            binding.tvSysName.text="智慧献血监护系统"
        }
    }


    override fun onBackPressed() {
    }
}

